package com.zmn.plat.business.impl.user;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.encode.EncodeUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.business.interfaces.user.UserBService;
import com.zmn.plat.manager.dictionary.UserDict;
import com.zmn.plat.model.entity.user.User;
import com.zmn.plat.model.entity.user.UserCount;
import com.zmn.plat.model.entity.user.UserDevice;
import com.zmn.plat.services.interfaces.user.UserCountService;
import com.zmn.plat.services.interfaces.user.UserDeviceService;
import com.zmn.plat.services.interfaces.user.UserService;

/**
 * Creator: nowind
 * 
 * @since 2018/7/13 13:55
 */
@Service
public class UserBServiceImpl implements UserBService {

    public final static String DEFAULT_PWD = "Qwe123456";

    @Resource
    private UserService userService;
    @Resource
    private UserDeviceService userDeviceService;
    @Resource
    private UserCountService userCountService;

    @Override
    public String updatePasswordForReset(int userId) {

        User user = userService.findByKey(userId);

        String defaultPwd = defaultPassword(user);
        this.updatePassword(userId, defaultPwd, GlobalDict.OPERATOR_TYPE_ADMIN);
        return defaultPwd;
    }

    @Override
    public Integer updatePassword(int userId, String password, int operatorType) {

        User user = new User();
        user.setUserId(userId);
        user.setPassword(password);
        user.setOperatorType(operatorType);

        return this.updatePassword(user);
    }

    @Override
    public Integer updatePassword(User user) {

        if (!StringUtil.isBlank(user.getPassword()) && user.getPassword().length() != 32) {
            user.setPassword(generatePassword(user.getPassword()));
        } else {
            return 0;
        }
        if (user.getUpdateTime() == null) {
            user.setUpdateTime(DateUtil.getNow());
        }
        return userService.updatePassword(user);
    }

    @Override
    public Integer insert(User user, String deviceCode, int deviceType, String registLatlng) {

        if (user.getGender() == 0) {
            user.setGender(GlobalDict.GENDER_NO);
        }
        if (user.getAuthMobile() == 0) {
            user.setAuthMobile(UserDict.AUTH_MOBILE_NO);
        }
        if (user.getAuthMail() == 0) {
            user.setAuthMail(UserDict.AUTH_MAIL_NO);
        }
        if (user.getEditName() == 0) {
            user.setEditName(UserDict.EDIT_NAME_YES);
        }
        if (user.getFocusWx() == 0) {
            user.setFocusWx(GlobalDict.YESNO_NO);
        }
        if (user.getStatus() == 0) {
            user.setStatus(GlobalDict.STATUS_ENABLE);
        }
        if (user.getRegistIp() == null) {
            user.setRegistIp("");
        }
        user.setRegistTime(DateUtil.getNow());
        user.setUpdateTime(user.getRegistTime());

        String defaultPwd = defaultPassword(user);
        user.setPassword(generatePassword(defaultPwd));

        int pos = userService.insert(user);

        if (pos > 0) {

            // 注册设备
            if (!StringUtil.isBlank(deviceCode)) {
                UserDevice device = new UserDevice();
                device.setUserId(user.getUserId());
                device.setDeviceCode(deviceCode);
                device.setDeviceType(deviceType);
                device.setRegistIp(user.getRegistIp());
                device.setRegistLatlng(registLatlng);
                device.setRegistTime(user.getRegistTime());
                userDeviceService.insert(device);
            }

            // 写入初始化用户数据
            UserCount count = new UserCount();
            count.setUserId(user.getUserId());
            return userCountService.insert(count);
        } else {
            return 0;
        }
    }

    // region 加密密码
    @Override
    public String generatePassword(String password) {
        return EncodeUtil.getMD5For32(password);
    }
    // endregion

    // region 默认密码
    private String defaultPassword(User user) {

        // 如用户已经有手机,则取手机后6位作为默认密码
        if (user.getMobile() > 0 && StringUtil.isMobile(String.valueOf(user.getMobile()))) {
            return String.valueOf(user.getMobile()).substring(5);
        } else {
            return DEFAULT_PWD;
        }
    }
    // endregion
}
